<template>
  <div class="register-container">
    <div class="register-box">
      <div class="logo">
        <h1>大麦电影票务平台</h1>
      </div>

      <el-form
          :model="form"
          :rules="rules"
          ref="registerForm"
          label-position="top"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              prefix-icon="User"
              clearable
          />
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
              prefix-icon="Iphone"
              maxlength="11"
              clearable
          />
        </el-form-item>

        <el-form-item label="年龄" prop="age">
          <el-input-number
              v-model="form.age"
              :min="12"
              :max="120"
              placeholder="请选择年龄"
              style="width: 100%"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
              v-model="form.confirmPassword"
              type="password"
              placeholder="请再次输入密码"
              prefix-icon="Lock"
              show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              class="register-btn"
              @click="submitForm"
              :loading="loading"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>

      <div class="footer-links">
        <!-- 优化这里：使用 router-link 代替点击事件 -->
        <router-link to="/login">
          <el-icon><ArrowLeft /></el-icon> 返回登录
        </router-link>
      </div>

      <div class="copyright">
        © 2023 大麦电影票务平台 版权所有
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import authService from '@/services/auth';
import { ElMessage } from 'element-plus';
import { ArrowLeft } from '@element-plus/icons-vue';
const router = useRouter();
const loading = ref(false);
const registerForm = ref(null);

const form = ref({
  username: '',
  phone: '',
  age: 18,
  password: '',
  confirmPassword: ''
});

// 自定义密码校验规则
const validatePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'));
  } else if (value.length < 6) {
    callback(new Error('密码长度不能少于6位'));
  } else {
    if (form.value.confirmPassword !== '') {
      registerForm.value.validateField('confirmPassword');
    }
    callback();
  }
};

// 自定义确认密码校验规则
const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== form.value.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

const rules = ref({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur'}
  ],
  phone: [
    {required: true, message: '请输入手机号', trigger: 'blur'},
    {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'}
  ],
  age: [
    {required: true, message: '请选择年龄', trigger: 'blur'}
  ],
  password: [
    {required: true, validator: validatePassword, trigger: 'blur'}
  ],
  confirmPassword: [
    {required: true, validator: validateConfirmPassword, trigger: 'blur'}
  ]
});

// 提交注册表单
const submitForm = async () => {
  try {
    // 验证表单
    await registerForm.value.validate();

    loading.value = true;

    // 调用注册API
    const response = await authService.register(form.value);

    if (response.code === 200) {
      ElMessage.success('注册成功，请登录');
      router.push('/login');
    } else {
      ElMessage.error(response.message || '注册失败');
    }
  } catch (error) {
    console.error('注册错误', error);
    if (error.response) {
      ElMessage.error(error.response.data.message || '注册失败');
    }
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.register-box {
  width: 420px;
  padding: 40px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.logo {
  margin-bottom: 25px;
}

.logo h1 {
  color: #e54847;
  font-size: 28px;
  letter-spacing: 2px;
  margin-bottom: 5px;
}

.register-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  margin-top: 15px;
  background-color: #e54847;
  border: none;
}

.register-btn:hover {
  background-color: #d03c3b;
}

.footer-links {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  font-size: 14px;
}

.footer-links a {
  color: #008cd6;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 5px;
}

.footer-links a:hover {
  text-decoration: underline;
}

.copyright {
  margin-top: 30px;
  color: #666;
  font-size: 12px;
}

/* 美化表单标签 */
:deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
  padding-bottom: 5px;
}
</style>